package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;
@Controller
public class indexController {
    @Autowired
    private UserService userService;

    @RequestMapping("/")
    public String index(ModelMap map){
        //加入一个属性，用来在模板中读取
        map.addAttribute("host","内蒙古电子");
        //return模板文件的名称，对应scr、main/resiurces/templates/index.html
        return "index";
    }
    @RequestMapping("echarts")
    public String echarts(){
        return "echarts";
    }

    @RequestMapping("echartx")
    public String echartx(ModelMap map){
        List<User> users=userService.queryAll();
        List<String> names=new ArrayList<>();
        List<Integer> ages=new ArrayList<>();
        if (users != null && users.size()>0){
            users.stream().forEach(user -> {
                names.add(user.getName());
                ages.add(user.getAge());
            });
        }
        map.addAttribute("names",names);
        map.addAttribute("ages",ages);
        return "echartx";
    }
}
